﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys
{
    public partial class uc_Locomotive_MainBase_TrainDepSys_Type : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "";
        public uc_Locomotive_MainBase_TrainDepSys_Type()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_TrainDepSys_Type_Load(object sender, EventArgs e)
        {
            _LoadGrid();
            _LoadTrainType();
        }

        private void _LoadTrainType()
        {
            TS_Win_Class.Fill_FLP_RadioBox(flp_Main, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0", tb_Name_TextChanged);
        }

        private void uc_Locomotive_MainBase_TrainDepSys_Type_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        private void _LoadGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,*,(select f_name from TS_S_T_P_TrainType where f_id=TS_S_T_P_TrainDepSys_Type.f_ttid) as f_ttname from TS_S_T_P_TrainDepSys_Type where f_del=0  and f_tdid=" + _LoadID;
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            _SetButtonEnbled();
        }
        private void _SetButtonEnbled()
        {
            bt_Add.Enabled = false;
            bt_Edit.Enabled = false;
            bt_Del.Enabled = false;
            string sf_ttid = "";
            foreach (Control _c in flp_Main.Controls)
            {
                if ((_c as RadioButton).Checked)
                {
                    sf_ttid = (_c as RadioButton).Tag.ToString();
                }
            }

            if (tb_Name.Text.Trim().Length > 0 && sf_ttid.Length > 0 && tb_Indate.Text.Trim().Length > 0 && tb_Num.Text.Trim().Length > 0)
            {
                bt_Add.Enabled = true;
                if (gv_Main.SelectedRowsCount > 0)
                {
                    bt_Edit.Enabled = true;
                    bt_Del.Enabled = true;
                }
            }
        }
        private void bt_Add_Click(object sender, EventArgs e)
        {
            string sf_ttid = "";
            foreach(Control _c in flp_Main.Controls)
            {
                if ((_c as RadioButton).Checked)
                {
                    sf_ttid = (_c as RadioButton).Tag.ToString();
                }
            }
            string strSQL = "insert into TS_S_T_P_TrainDepSys_Type(f_name,f_num,f_indate,f_ttid,f_tdid,f_lownum,f_upnum,f_workerid) values('" + tb_Name.Text.Trim() + "'," + tb_Num.Text.Trim() + "," + tb_Indate.Text.Trim() + ","+ sf_ttid + ","+_LoadID+ "," + tb_Low.Text.Trim() + "," + tb_Up.Text.Trim() + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string sf_ttid = "";
            foreach (Control _c in flp_Main.Controls)
            {
                if ((_c as RadioButton).Checked)
                {
                    sf_ttid = (_c as RadioButton).Tag.ToString();
                }
            }
            string strSQL = "update TS_S_T_P_TrainDepSys_Type set f_name='" + tb_Name.Text.Trim()+ "',f_num=" + tb_Num.Text.Trim() + ",f_indate=" + tb_Indate.Text.Trim() + ",f_ttid=" + sf_ttid + ",f_lownum=" + tb_Low.Text.Trim() + ",f_upnum=" + tb_Up.Text.Trim() + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_TrainDepSys_Type set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                tb_Name.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_name").ToString();
                tb_Num.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_num").ToString();
                tb_Indate.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_indate").ToString();
                tb_Low.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_lownum").ToString();
                tb_Up.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_upnum").ToString();
                foreach (Control _c in flp_Main.Controls)
                {
                    if ((_c as RadioButton).Tag.ToString()== gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_ttid").ToString())
                    {
                        (_c as RadioButton).Checked = true;
                    }
                }
            }
            else
            {
                tb_Name.Text ="";
                tb_Num.Text = "0";
                tb_Indate.Text = "0";
                tb_Low.Text = "0";
                tb_Up.Text = "0";
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}